package org.lonetree.tc.webapp.actions;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.lonetree.tc.core.User;
import org.lonetree.tc.webapp.forms.UserForm;

public class UpdateMyProfile extends SecureAction {

	@Override
	protected ActionForward executeSecure(ActionMapping mapping,
			ActionForm form, HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		
		try{
			User u = getCurrentUser();
			UserForm f = (UserForm)form;
			f.setEmail(u.getEmail());
			f.setAdmin(u.isAdmin());
			f.setFirstName(u.getFirstName());
			f.setLastName(u.getLastName());
			f.setPassword(u.getPassword());
			f.setUsername(u.getUsername().getUsernameString());
			f.setLocation(u.getLocation());
			request.setAttribute("update.action", new Object());
			request.setAttribute("UserForm", u);
			return mapping.findForward("success");
		}
		catch(Exception e){
			e.printStackTrace();
			return mapping.findForward("failure");
		}
	}

}
